package com.open.demo.span;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.open.demo.R;
import com.open.demo.span.bean.SpanBean;

import java.util.ArrayList;
import java.util.List;

/**
 * ImageSpan
 * ClickableSpan
 * TypefaceSpan
 */
@SuppressLint("AppCompatCustomView")
public class TestTextView extends TextView {

    public TestTextView(final Context context) {
        super(context);
    }

    public TestTextView(final Context context, @Nullable final AttributeSet attrs) {
        super(context, attrs);
    }

    public TestTextView(final Context context, @Nullable final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    List<SpanBean> mSpanBeans = new ArrayList<>();

    private void initTestDatas() {
        // SpanAnimInflater.loadFontAnim(getResources(), R.xml.font_anim_test);
        // Animator animator = AnimatorInflater.loadAnimator(getContext(), R.animator.test_animator);
//        animator.setTarget(this);
//        animator.start();

        mSpanBeans = new ArrayList<>();
        SpanBean spanBean1 = new SpanBean("我", 0, 1);
        SpanBean spanBean2 = new SpanBean("爱你", 1, 3);
        SpanBean spanBean3 = new SpanBean("中", 3, 4);
        SpanBean spanBean4 = new SpanBean("国", 4, 5);
        spanBean1.setPropertyName("Alpha");
        spanBean1.setTextColor("#FF0000");
        spanBean1.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"ttf/"+"08华康娃娃体W5.TTF"));
        spanBean2.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"ttf/"+"04汉仪铸字童年体.ttf"));
        spanBean3.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"ttf/"+"20张海山锐线体2.0.ttf"));
        spanBean4.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"ttf/"+"05汉仪乐喵字体.ttf"));
        //
        mSpanBeans.add(spanBean1);
        mSpanBeans.add(spanBean2);
        mSpanBeans.add(spanBean3);
        mSpanBeans.add(spanBean4);
        // test 字体样式
//        setTypeface(Typeface.createFromAsset(getContext().getAssets(),"ttf/"+"20张海山锐线体2.0.ttf"));
    }

    public void setSpanDatas() {
    }

    @Override
    public void setText(final CharSequence text, final BufferType type) {
        initTestDatas();
        //
        SpannableStringBuilder builder = new SpannableStringBuilder("我爱你中国");
        for (SpanBean spanBean : mSpanBeans) {
            FontAnimReplacementSpan fontAnimReplacementSpan = new FontAnimReplacementSpan(this);
            fontAnimReplacementSpan.setColor(Color.parseColor(spanBean.getTextColor()));
            fontAnimReplacementSpan.mTypeface = spanBean.getTypeface();
            builder.setSpan(fontAnimReplacementSpan, spanBean.getStartIndex(), spanBean.getEndIndex(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

        super.setText(builder, type);

        // 动画DEMO.
        FontAnimReplacementSpan[] singleSpans = builder.getSpans(0, builder.length(), FontAnimReplacementSpan.class);
        if (singleSpans.length > 0) {
            AnimatorSet allAnimSet = new AnimatorSet();
            //
            ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(singleSpans[0], "alpha",0.1f, 1.0f);
            alphaAnimator.setDuration(5000);
            //
            ObjectAnimator scaleXAnimator = ObjectAnimator.ofFloat(singleSpans[1], "scaleX", 1.0f, 2.0f, 1.5f, 1.0f);
            ObjectAnimator scaleYAnimator = ObjectAnimator.ofFloat(singleSpans[1], "scaleY", 1.0f, 2.0f, 1.5f, 1.0f);
            ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(singleSpans[1], "rotate",0, 360);
            AnimatorSet scaleAllSet = new AnimatorSet();
            scaleAllSet.playTogether(scaleXAnimator, scaleYAnimator, rotateAnimator);
            scaleAllSet.setDuration(8000);
            //
            ObjectAnimator translateXAnimator = ObjectAnimator.ofFloat(singleSpans[2], "translateX", 0, 100, -100, 0);
            translateXAnimator.setDuration(8888);
            //
            ObjectAnimator colorAnimator = ObjectAnimator.ofInt(singleSpans[3], "color", Color.BLUE, Color.RED, Color.BLUE);
            colorAnimator.setDuration(8888);
            //
            allAnimSet.playTogether(alphaAnimator, scaleAllSet, translateXAnimator, colorAnimator);
            allAnimSet.start();
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    private <T> void remove(SpannableStringBuilder sb, Class<T> c){
        T[] spans = sb.getSpans(0,sb.length(), c);
        if(spans!=null){
            for (int i = 0; i < spans.length; i++) {
                sb.removeSpan(spans[i]);
            }
        }
    }

}
